$(function (){
    $("button.bianji").click(function () {
        $("div.mask-box").show();
        $("div.dialog-box").show();
        var tdNode = $(this).parent();//得到td
        var trNode = tdNode.parent();
        var ee=trNode.find("td.id").text()
        f(ee);//得到tr
    });
    function f(ee) {

        $.ajax({
            type: 'GET',
            url : 'http://localhost:8080/order/update?id='+ee,
            dataType:'json',
            success:(function(m) {
                data= m.order;
                $("#orderid").val(data.orderid);
                $("#userid").val(data.userid);
                console.log(data.orderDate);
                var dm=new Date(data.orderDate);
                $("#orderdate").val(dm.getFullYear()+"-"+(dm.getMonth()+1).toString().padStart(2,'0')+"-"+dm.getDate().toString().padStart(2,'0'));
                $("#address").val(data.billAddr1);
                $("#name").val(data.billToFirstname);
                $("#accountnumber").val(data.creditCard);
                $("#accountmethod").val(data.cardType);
                $("#goods").val();
                //$("#price").val(data.totalPrice);
                $("#address1").val(data.shipAddr1);
                $("#name2").val(data.shipToFirstname);
                item2=m.item;

                for(var i=0;i<item2.length;i++)
                {console.log(item2.length);
                    var d1=document.createElement("div");
                    var d2=document.createElement("div");
                    var e_0 = document.createElement("div");
                    e_0.setAttribute("class", "tiao");
                    var e_1 = document.createElement("div");
                    e_1.setAttribute("class", "tu");
                    e_0.appendChild(e_1);
                    var e_2 = document.createElement("div");
                    e_2.setAttribute("class", "wen");
                    var e_3 = document.createElement("input");
                    e_3.setAttribute("type", "text");
                    e_3.setAttribute("style", "width: 60px; ");
                    var s0="bianjiitem"+(i+1);
                    e_3.setAttribute("id", s0);
                    e_3.setAttribute("value", item2[i].itemId);
                    d1.appendChild(e_3);
                   // e_2.appendChild(e_3);
                    var e_4 = document.createElement("label");
                    e_4.setAttribute("htmlfor", "");
                    e_4.appendChild(document.createTextNode("X"));
                    d1.appendChild(e_4);
                    //e_2.appendChild(e_4);
                    var e_5 = document.createElement("input");
                    e_5.setAttribute("type", "text");
                    e_5.setAttribute("style", "width: 20px;");
                    e_5.setAttribute("value", item2[i].quantity);
                    var s1="shuliangitem"+(i+1);
                    e_5.setAttribute("id", s1);
                    d1.appendChild(e_5);
                    //e_2.appendChild(e_5);
                    e_2.appendChild(d1);
                    var e8=document.createElement("label");
                    e8.setAttribute("style","width:70px")
                    var jj="price"+(i+1);
                    e8.setAttribute("id",jj);
                    //e8.setAttribute("value",)
                    e8.innerText=item2[i].unitPrice;
                    d2.appendChild(e8);
                    var e9=document.createElement("label");
                    e9.appendChild(document.createTextNode("$/个"));
                    d2.appendChild(e9);
                    e_2.appendChild(d2);
                    //e_2.appendChild(e8);
                    e_0.appendChild(e_2);
                    var e_6 = document.createElement("div");
                    e_6.setAttribute("class", "an");
                    var e_7 = document.createElement("button");
                    e_7.setAttribute("class", "jia");
                    var s2="jiashu"+(i+1);
                    e_7.setAttribute("id", s2);
                    e_7.appendChild(document.createTextNode("+"));
                    e_6.appendChild(e_7);
                    var e_8 = document.createElement("button");
                    e_8.setAttribute("class", "jia");
                    var s3="jianshu"+(i+1);
                    e_8.setAttribute("id", s3);
                    e_8.appendChild(document.createTextNode("-"));
                    e_6.appendChild(e_8);
                    e_0.appendChild(e_6);
                    var e_9 = document.createElement("div");
                    e_9.setAttribute("class", "xiao");
                    var s4="xiaochu"+(i+1);
                    e_9.setAttribute("id", s4);
                    var e_10 = document.createElement("button");
                    e_10.setAttribute("class", "de");
                    e_10.appendChild(document.createTextNode("X"));
                    e_9.appendChild(e_10);
                    e_0.appendChild(e_9);
                    var ss1="#"+"bianjiitem"+(i+1);
                    var ss2="#"+"shuliangitem"+(i+1);
                    var ss3="jiashu"+(i+1);
                    var ss4="jianshu"+(i+1);
                    var ss5="xiaochu"+(i+1);
                    var ss6="#"+jj;
                    $("#wai").append(e_0);
                    var de;
                    if($("#price").val()===null)
                    {
                        de=0;
                    }
                    else{
                        de=Number($("#price").val());
                    }
                    $("#price").val(de+Number(item2[i].quantity)*Number(item2[i].unitPrice))
                    console.log(de);
                    console.log($(ss2).val());


                    //使用闭包函数
                    document.getElementById(ss3).onclick = (function(n,m){

                        return function() {

                            $(n).val(Number($(n).val())+1);
                            $("#price").val(Number($("#price").val())+Number($(m).html()));


                        }}(ss2,ss6));
                    document.getElementById(ss4).onclick = (function(n,m){

                        return function() {

                            $(n).val(Number($(n).val())-1);
                            $("#price").val(Number($("#price").val())-Number($(m).html()));

                        }}(ss2,ss6));
                    document.getElementById(ss5).onclick = (function(n){
                        return function() {

                            $.ajax({
                                type: 'POST',
                                url : 'http://localhost:8080/order/orderless',
                                data:  {"id":ee,"item":$(n).val()},
                                dataType:'text',
                                success:(function(data){
                                    alert(data);

                                }),
                                error:(function (data){alert(data);})

                            });
                        }}(ss1));


                    $("#"+ss5).click(function (e) {
                        var Node = $(this).parent();
                        Node.remove();
                    });
                }
            }),
            error:(function (data){})
        });

    };
    $("button#close").click(function (){
        $("#wai").html("");
        $("div.mask-box").hide();
        $("div.dialog-box").hide();
        $("#price").val("");

    });
    $("button.delete").on("click",function (e){
        var judge = confirm("确认删除吗?")
        e.stopPropagation();
        e.preventDefault();

        if (judge === true)
        {
            var tdNode = $(this).parent();//得到td
            var trNode = tdNode.parent();//得到tr

            $.ajax({
                type: 'POST',
                url : 'http://localhost:8080/order/delete',
                data:  {"id":trNode.find("td.id").text()},
                dataType:'text',
                success:(function(data){
                    trNode.remove();
                    alert(data);}),
                error:(function (data){alert(data);})

            });
        }
        else{}

    });
    $("button.fahuo").on("click",function (e){
        // e.stopPropagation();
        // e.preventDefault();

            var tdNode = $(this).parent();//得到td
            var trNode = tdNode.parent();//得到tr
            console.log();
        trNode.find('td.fa').text("已发货");

            // $.ajax({
            //     type: 'POST',
            //     url : 'http://localhost:8080/order/fahuo',
            //     data:  {"id":trNode.find("td.id").text()},
            //     dataType:'text',
            //     success:(function(data){
            //         trNode.remove();
            //         alert(data);}),
            //     error:(function (data){alert(data);})
            //
            // });



    });
    $("#bianjitijiao").click(function () {
        var data=new Object();
        data.orderid=$("#orderid").val();
        data.userid=$("#userid").val();
        data.orderdate=$("#orderdate").val();
        data.billAddr1=$("#address").val();
        data.billToFirstname=$("#name").val();
        data.creditCard=$("#accountnumber").val();
        data.cardType=$("#accountmethod").val();
        data.totalPrice=$("#price").val();
        data.shipAddr1=$("#address1").val();
        data.shipToFirstname=$("#name2").val();
        var orderp=[];
        orderp.push(data);
        var m=$(".xiao").length;
        for(var i=0;i<m;i++)
        {
            var on="#"+"bianjiitem"+(i+1);
            var oo="#"+"shuliangitem"+(i+1);
            var one=$(on).val();//获取订单物品名称
            var sh=$(oo).val();//获取订单物品的数量
            var zx={"name":one,"shuliang":sh};
            orderp.push(zx);
            console.log(orderp[0])

        }
        $.ajax({
            type: 'POST',
            url : 'http://localhost:8080/order/bianji',
            data: JSON.stringify(orderp) ,
            contentType:"application/json",
            dataType:'text',
            success:(function(data){

                alert(data);}),
            error:(function (data){alert(data);})

        });

        var url='/OrderForm';
        var form = $("<form method='get'></form>");
        form.attr({"action":url});
        var input = $("<input type='hidden'>");
        form.append(input);
        $("#submitForm").append(form);
        form.submit();
        $("#submitForm").html("");

    });
    $("#additem").focus(function () {

   $("#leftMenu").show();

    });
    // $("#additem").blur(function () {
    //     $("#leftMenu").hide();
    // });
    // $(".clitem").on('click',function () {
    //     console.log("111");
    //     $("#additem").val($(this).innerText);
    // });
    $("#leftMenu").on('click',".clitem",function(){

        $("#additem").val($(this).text());
        $("#leftMenu").hide();
        console.log();
    });
    $("#tianjia").click(function () {
        var si= $("#additem").val();
        var s3=$("#orderid").val();
        $("#price").val("");
        $.ajax({
            type: 'GET',
            url : 'http://localhost:8080/order/tianjia?id='+si+'&orderid='+s3,
            dataType:'text',
            success:(function(data){
                $("#wai").html("");
                var judge2 = confirm("确认添加吗?")
                if(judge2===true)
                {
                    f(s3);
                    $("#additem").val("");
                }
            }),
            error:(function (data){})
        });



    });

});